/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package etrl.core;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * \class Group
 * \brief Intermediate scene graph node that can have childrens.
 * @author harold
 *
 */
public class Group extends Node
{
	/**
	 * List of childrens
	 */
	private ArrayList <Node> _childrens;
	
	/**
	 * \brief Initializes an empty group.
	 */
	public Group()
	{
		super();
	
		_childrens = new ArrayList <Node>();
	}
	
	/**
	 * \brief Adds a given child to this group.
	 * @param[in] child chilren to add.
	 */
	public void addChild(Node child)
	{
		_childrens.add(child);
	}
	
	/**
	 * \brief Removes the child at the given index in child list
	 * @param[in] index index of the child to remove
	 */
	public void removeChild(int index)
	{
		_childrens.remove(index);
	}
	
	/**
	 * \brief Removes all childrens
	 */
	public void removeChilds()
	{
		_childrens.clear();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void accept(NodeVisitor nodeVisitor)
	{
		nodeVisitor.visit(this);
	}
	
	/**
	 * \brief Makes the given node visitor visiting all childs
	 * @param[in] nodeVisitor node visitor which must visit childwood
	 */
	public void traverse(NodeVisitor nodeVisitor)
	{
		for(Iterator <Node> it = _childrens.iterator(); it.hasNext();)
		{
			it.next().accept(nodeVisitor);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public BoundingSphere computeBoundingSphere()
	{
		// Get a new bounding sphere
		BoundingSphere bsphere = new BoundingSphere();
		
		// And extend it with the bounding sphere of each child
		for(Iterator <Node> it = _childrens.iterator(); it.hasNext();)
		{
			bsphere.expandBy(it.next().getBounds());
		}
		
		return bsphere;
	}
}
